package com.woan.api.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.services.MessageServerServices;
import com.woan.api.vo.PushMessage;
import com.woan.api.vo.PushMessageType;
import com.woan.api.vo.Pushjson;

import net.sf.json.JSONObject;


public class PushAsync {
	HttpTool ht = new HttpTool();
	private String fristname;
	private String username;
	private String msg;
	private List pushList;
	private String type;
	private String msgtype;
	public PushAsync(String fristname,String username,String msg,List pushList,String type,String msgtype) {
		this.fristname = fristname;
		this.username = username;
		this.msg = msg;
		this.pushList = pushList;
		this.type=type;
		this.msgtype=msgtype;
	}
	public static List removeDuplicateWithOrder(List list) {
		Set set = new HashSet();
		List newList = new ArrayList();
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (set.add(element))
				newList.add(element);
		}
		return newList;
	}
	public void iosPush(String msg,String username,List pushList,String msgtype)
	{
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		IphonePush send=new IphonePush();
		List<String> tokens=new ArrayList<String>();
		for(int i=0; i<pushList.size(); i++)    
		{
			if(pushList.get(i).toString().length() == 64)
			{
				System.out.println(pushList.get(i).toString());
				tokens.add(pushList.get(i).toString().replaceAll(" ", ""));
			}
		}
		tokens= removeDuplicateWithOrder(tokens);
		System.out.println("苹果推送设备的长度"+tokens.size());
		if(tokens.size()>0)
		{
			System.out.println("ios 推送开始");
			String filename = rb_config.getString("filename");
			String password=rb_config.getString("password");
			boolean toserver = Boolean.parseBoolean(rb_config.getString("toserver"));
			String sendcoutent="";
			if(msgtype.equals("zbstart"))
				sendcoutent = "{'aps':{'alert':'"+username+"正在拍摄视频。'},'type':'0','url':'"+msg+"'}";
			if(msgtype.equals("voicemsg"))
				sendcoutent="{'aps':{'alert':'成员"+username+"的语音消息'},'type':'1','url':'"+msg+"'}";
			if(msgtype.equals("groupmestext"))
				sendcoutent = "{'aps':{'alert':'"+username+"说:"+msg+"'},'type':'2','url':''}";
			send.sendpush(tokens, filename, password, sendcoutent, 1, false,toserver);
		}
	}
	public void androidPush(String msg,String groupname,String username,List pushList,String msgtype)
	{
		
		/*PushMessage pushmes = new PushMessage();
		PushMessageType pushmestype = new PushMessageType();
		Pushjson rjson = new Pushjson();
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		pushmes.setGroupname(groupname);
		pushmes.setUsername(username);
		pushmes.setTitle(msg);
		pushmes.setContent("");
		String curtime = FuncDate.FindCurSysTime1();
		pushmes.setDatetime(curtime);
		pushmestype.setMegtype(msgtype);
		pushmestype.setPushMessage(pushmes);
		rjson.setResults("0");
		rjson.setItems(pushmestype);
		rjson.setErrMsg("");
		String pushmsg = JSONObject.fromObject(rjson).toString();
		pushmsg = pushmsg.replaceAll("\\\\", "");
		String androidpushlist="";
		for(int i=0; i<pushList.size(); i++)    
		{
			System.out.println(pushList.get(i).toString());
			androidpushlist = androidpushlist +pushList.get(i).toString()+",";
			
		}
		if(!androidpushlist.equals(""))
			androidpushlist = androidpushlist.substring(0,androidpushlist.length()-1);
		System.out.println("android push消息人员列表="+androidpushlist);

		 
		 if(!androidpushlist.equals(""))
		 {
			 System.out.println("Android 手机开始---------------");
			 String broadcast = "N";
			 String resource = rb_config.getString("androidsource");
			 String postaddress = rb_config.getString("androidpush");
			 String rest = ht.sendPostHttpRequest(postaddress, "broadcast="
					+ broadcast + "&resource=" + resource
					+ "&username=" + androidpushlist + "&content=" + pushmsg);
		 }*/
	}
	public void send() {

		// 新定义线程来处理发送
		System.out.println("线程开始了！！！！！！！！");
		new Thread() {
			public void run() {
				if(type.equals("ios"))
					//iosPush(msg,username,pushList,msgtype);
					androidPush(msg,fristname,username,pushList,msgtype);
				if(type.equals("android"))
					androidPush(msg,fristname,username,pushList,msgtype);
			}
		}.start();

	}
	public static void main(String[] args) {

//		String prtNo = "1001200912310155555";
//
//		// 调用异步发送短信
//
//		SmsSendAsync sendAsync = new SmsSendAsync(prtNo);
//
//		sendAsync.send();
//
//		System.out.println("已经签单");

	}
}
